package com.treasure.mall.biz.entity.live;

import com.treasure.mall.biz.dto.live.SessionStatisticsDTO;
import com.treasure.mall.biz.entity.BasePO;
import com.treasure.mall.zbq.common.Jsr310Utils;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.ienum.live.LiveEnums;
import lombok.Data;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 
 * 直播场次
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.entity.live
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/11 18:45
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.biz.entity.live Inc. All rights reserved.
 */
@Entity
@Data
@Table(name = "zb_live_session")
@Cacheable(false)
public class LiveSessionPO extends BasePO {


    @Id
    @Column(name = "id", length = 30)
    private String id;
    /**
     * 当前场次名称
     */
    @Column(name = "session_title")
    private String sessionTitle;
    @ManyToOne
    @JoinColumn(name = "live_room_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private LiveRoomPO liveRoomPO;
    /**
     * 主持人
     */
    @ManyToOne
    @JoinColumn(name = "director_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private LiveDirectorPO liveDirectorPO;
    /**
     * 播流地址，每次获取之后确保1h内可用。
     */
    @Column(name = "play_url")
    private String playUrl;

    /**
     * 直播状态
     */
    @Column(name = "play_status")
    private LiveEnums.PlayStatus playStatus;
    /**
     * 最高在线人数
     */
    @Column(name = "max_online_num")
    private long maxOnlineNum;
    /**
     * 累计人数
     */
    @Column(name = "current_max_visits")
    private long currentMaxVisits;
    /**
     * 成交订单数量
     */
    @Column(name = "deal_num")
    private long dealNum;
    /**
     * 活跃度
     */
    @Column(name = "activity_num")
    private long activityNum;
    /**
     * 成交金额
     */
    @Column(name = "deal_amount")
    private BigDecimal dealAmount = BigDecimal.ZERO;
    /**
     * 预告时间
     */
    @Column(name = "foreshow_time", columnDefinition = Constant.DATE_COLUMN_DEFINITION)
    private LocalDateTime foreshowTime;
    /**
     * 预告内容
     */
    @Column(name = "foreshow_desc")
    private String foreshowDesc;
    /**
     * 开始时间
     */
    @Column(name = "start_time", columnDefinition = Constant.DATE_COLUMN_DEFINITION)
    private LocalDateTime startTime;
    /**
     * 结束时间
     */
    @Column(name = "end_time", columnDefinition = Constant.DATE_COLUMN_DEFINITION)
    private LocalDateTime endTime;
    @Transient
    private SessionStatisticsDTO statisticsDTO;

    /**
     * 开启机器人
     */
    @Column(name = "enable_robot")
    private Boolean enableRobot = false;

    /**
     * 用户停留总时长
     */
    @Column(name = "user_online_total_seconds")
    private long userOnlineTotalSeconds;

    /**
     * 直播时长，单位秒
     *
     * @return
     */
    public Long liveSeconds() {
        if (this.endTime != null) {
            return Duration.between(this.startTime, this.endTime).getSeconds();
        }
        return 0L;
    }

    public String liveDuration() {
        return Jsr310Utils.Duration.getDurationBySeconds(liveSeconds());
    }

    public String getAvgDuration() {
        if (userOnlineTotalSeconds == 0) {
            return Jsr310Utils.Duration.getDurationBySeconds(userOnlineTotalSeconds);
        }
        return Jsr310Utils.Duration.getDurationBySeconds(userOnlineTotalSeconds / currentMaxVisits);
    }
}


